package com.example.classmanageapp;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.classmanageapp.DataProvider.ClassInfoDBHelper;
import com.example.classmanageapp.DataProvider.ClassInfoSharedPreferences;
import com.example.classmanageapp.utils.SysUtils;
import com.lidroid.xutils.ViewUtils;

public class ClassActivity extends Activity {

    private Context context;
    // SharedPreference与SQLite的访写类
    private ClassInfoDBHelper classInfoDBHelper;
    private ClassInfoSharedPreferences classInfoSharedPreferences;

    private int classIndex = 0;

    Spinner sadd_class_week;
    Button class_mon_6_btn;
    Button class_mon_5_btn;
    Button class_mon_4_btn;
    Button class_mon_3_btn;
    Button class_mon_2_btn;
    Button class_mon_1_btn;

    TextView class_mon_6_text;
    TextView class_mon_5_text;
    TextView class_mon_4_text;
    TextView class_mon_3_text;
    TextView class_mon_2_text;
    TextView class_mon_1_text;
    TextView class_txt;

    TextView class_tue_6_text;
    TextView class_tue_5_text;
    TextView class_tue_4_text;
    TextView class_tue_3_text;
    TextView class_tue_2_text;
    TextView class_tue_1_text;
    private String[][] s1 = {
            {"语文", "有课", "无"},
            {"数学", "有课", "无"},
            {"英语", "有课", "无"},
            {"计算机科学与技术", "有课", "无"},
            {"无", "无", "安排课程"},
            {"语文", "有课", "无"},
    };

    private String[][] s2 = {
            {"无", "无", "安排课程"},
            {"数学", "有课", "无"},
            {"英语", "有课", "无"},
            {"计算机科学与技术", "有课", "无"},
            {"无", "无", "安排课程"},
            {"语文", "有课", "无"},
    };

    private String[][] s3 = {
            {"计算机科学与技术", "有课", "无"},
            {"无", "无", "安排课程"},
            {"英语", "有课", "无"},
            {"英语", "有课", "无"},
            {"无", "无", "安排课程"},
            {"数学", "有课", "无"},
    };

    private String[] s4 = {"计算机楼-实验室-1001", "计算机楼-实验室-1002", "计算机楼-实验室-1003"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SysUtils.setStatusBarColor(this, true, 0);
        SysUtils.setSkin(this);
        setContentView(R.layout.activity_class);
        ViewUtils.inject(this);
        context = this;
        classIndex = 0;
        classInfoDBHelper = new ClassInfoDBHelper(ClassActivity.this, "Classes.db", null, 1);
        // 初始化数据存储类 并初始化数据
        classInfoSharedPreferences = new ClassInfoSharedPreferences(ClassActivity.this);
        initData();
        class_mon_1_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_1_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 0, sadd_class_week.getSelectedItemPosition());
                }
            }
        });
        class_mon_2_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_2_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 1, sadd_class_week.getSelectedItemPosition());
                }
            }
        });
        class_mon_3_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_3_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 2, sadd_class_week.getSelectedItemPosition());
                }
            }
        });
        class_mon_4_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_4_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 3, sadd_class_week.getSelectedItemPosition());
                }
            }
        });
        class_mon_5_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_5_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 4, sadd_class_week.getSelectedItemPosition());
                }
            }
        });
        class_mon_6_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("无".equals(class_mon_6_btn.getText().toString())) {
                    Toast.makeText(ClassActivity.this, "无效操作!!!", Toast.LENGTH_SHORT).show();
                }else {
                    addClass(s4[classIndex], 5, sadd_class_week.getSelectedItemPosition());
                }
            }
        });

    }

    private void initData() {


        sadd_class_week = findViewById(R.id.add_class_week);
        class_mon_6_btn = findViewById(R.id.class_mon_6_btn);
        class_mon_5_btn = findViewById(R.id.class_mon_5_btn);
        class_mon_4_btn = findViewById(R.id.class_mon_4_btn);
        class_mon_3_btn = findViewById(R.id.class_mon_3_btn);
        class_mon_2_btn = findViewById(R.id.class_mon_2_btn);
        class_mon_1_btn = findViewById(R.id.class_mon_1_btn);

        class_mon_1_text = findViewById(R.id.class_mon_1_text);
        class_mon_2_text = findViewById(R.id.class_mon_2_text);
        class_mon_3_text = findViewById(R.id.class_mon_3_text);
        class_mon_4_text = findViewById(R.id.class_mon_4_text);
        class_mon_5_text = findViewById(R.id.class_mon_5_text);
        class_mon_6_text = findViewById(R.id.class_mon_6_text);

        class_tue_1_text = findViewById(R.id.class_tue_1_text);
        class_tue_2_text = findViewById(R.id.class_tue_2_text);
        class_tue_3_text = findViewById(R.id.class_tue_3_text);
        class_tue_4_text = findViewById(R.id.class_tue_4_text);
        class_tue_5_text = findViewById(R.id.class_tue_5_text);
        class_tue_6_text = findViewById(R.id.class_tue_6_text);

        class_txt = findViewById(R.id.class_txt);

        class_mon_1_text.setText(s1[0][0]);
        class_mon_2_text.setText(s1[1][0]);
        class_mon_3_text.setText(s1[2][0]);
        class_mon_4_text.setText(s1[3][0]);
        class_mon_5_text.setText(s1[4][0]);
        class_mon_6_text.setText(s1[5][0]);

        class_tue_1_text.setText(s1[0][1]);
        class_tue_2_text.setText(s1[1][1]);
        class_tue_3_text.setText(s1[2][1]);
        class_tue_4_text.setText(s1[3][1]);
        class_tue_5_text.setText(s1[4][1]);
        class_tue_6_text.setText(s1[5][1]);

        class_mon_1_btn.setText(s1[0][2]);
        class_mon_2_btn.setText(s1[1][2]);
        class_mon_3_btn.setText(s1[2][2]);
        class_mon_4_btn.setText(s1[3][2]);
        class_mon_5_btn.setText(s1[4][2]);
        class_mon_6_btn.setText(s1[5][2]);

    }

    public void onclick1(View view) {
        classIndex = 0;
        class_mon_1_text.setText(s1[0][0]);
        class_mon_2_text.setText(s1[1][0]);
        class_mon_3_text.setText(s1[2][0]);
        class_mon_4_text.setText(s1[3][0]);
        class_mon_5_text.setText(s1[4][0]);
        class_mon_6_text.setText(s1[5][0]);

        class_tue_1_text.setText(s1[0][1]);
        class_tue_2_text.setText(s1[1][1]);
        class_tue_3_text.setText(s1[2][1]);
        class_tue_4_text.setText(s1[3][1]);
        class_tue_5_text.setText(s1[4][1]);
        class_tue_6_text.setText(s1[5][1]);

        class_mon_1_btn.setText(s1[0][2]);
        class_mon_2_btn.setText(s1[1][2]);
        class_mon_3_btn.setText(s1[2][2]);
        class_mon_4_btn.setText(s1[3][2]);
        class_mon_5_btn.setText(s1[4][2]);
        class_mon_6_btn.setText(s1[5][2]);

        class_txt.setText("当前教室：" + s4[classIndex] + "   ");
    }

    public void onclick2(View view) {
        classIndex = 1;
        class_mon_1_text.setText(s2[0][0]);
        class_mon_2_text.setText(s2[1][0]);
        class_mon_3_text.setText(s2[2][0]);
        class_mon_4_text.setText(s2[3][0]);
        class_mon_5_text.setText(s2[4][0]);
        class_mon_6_text.setText(s2[5][0]);

        class_tue_1_text.setText(s2[0][1]);
        class_tue_2_text.setText(s2[1][1]);
        class_tue_3_text.setText(s2[2][1]);
        class_tue_4_text.setText(s2[3][1]);
        class_tue_5_text.setText(s2[4][1]);
        class_tue_6_text.setText(s2[5][1]);

        class_mon_1_btn.setText(s2[0][2]);
        class_mon_2_btn.setText(s2[1][2]);
        class_mon_3_btn.setText(s2[2][2]);
        class_mon_4_btn.setText(s2[3][2]);
        class_mon_5_btn.setText(s2[4][2]);
        class_mon_6_btn.setText(s2[5][2]);

        class_txt.setText("当前教室：" + s4[classIndex] + "   ");
    }

    public void onclick3(View view) {
        classIndex = 2;
        class_mon_1_text.setText(s3[0][0]);
        class_mon_2_text.setText(s3[1][0]);
        class_mon_3_text.setText(s3[2][0]);
        class_mon_4_text.setText(s3[3][0]);
        class_mon_5_text.setText(s3[4][0]);
        class_mon_6_text.setText(s3[5][0]);

        class_tue_1_text.setText(s3[0][1]);
        class_tue_2_text.setText(s3[1][1]);
        class_tue_3_text.setText(s3[2][1]);
        class_tue_4_text.setText(s3[3][1]);
        class_tue_5_text.setText(s3[4][1]);
        class_tue_6_text.setText(s3[5][1]);

        class_mon_1_btn.setText(s3[0][2]);
        class_mon_2_btn.setText(s3[1][2]);
        class_mon_3_btn.setText(s3[2][2]);
        class_mon_4_btn.setText(s3[3][2]);
        class_mon_5_btn.setText(s3[4][2]);
        class_mon_6_btn.setText(s3[5][2]);

        class_txt.setText("当前教室：" + s4[classIndex] + "   ");
    }

    public void clickFinish(View view) {
        finish();
    }
    // 弹出窗口添加课程
    private void addClass(String inputPlace, int section, int classWeek) {
        LayoutInflater factory = LayoutInflater.from(ClassActivity.this);
        final View view = factory.inflate(R.layout.add_class_layout, null);
        final EditText classNameTextView = (EditText) view.findViewById(R.id.add_class_input_name);
        final EditText classTeacherTextView = (EditText) view.findViewById(R.id.add_class_input_teacher);
        final EditText classPlaceTextView = (EditText) view.findViewById(R.id.add_class_input_place);
        classPlaceTextView.setText(inputPlace);
        final Spinner classSeqSpinner = (Spinner) view.findViewById(R.id.add_class_class_seq);
        classSeqSpinner.setSelection(section);
        final Spinner weekSeqSpinner = (Spinner) view.findViewById(R.id.add_class_class_week_seq);
        weekSeqSpinner.setSelection(classWeek);
        final Spinner classSemSpinner = (Spinner) view.findViewById(R.id.add_class_input_semester);
        final Spinner classStartSpinner = (Spinner) view.findViewById(R.id.add_class_input_start_week);
        final Spinner classEndSpinner = (Spinner) view.findViewById(R.id.add_class_input_end_week);
        if (classNameTextView.getText().toString().trim() == null || classTeacherTextView.getText().toString().trim() == null
                || classPlaceTextView.getText().toString().trim() == null || classNameTextView.getText().toString().trim() == ""
                || classTeacherTextView.getText().toString().trim() == "" || classPlaceTextView.getText().toString().trim() == "") {
            Toast.makeText(ClassActivity.this, "先把信息填写完整哦", Toast.LENGTH_SHORT).show();
            return;
        }


        AlertDialog dialog = new AlertDialog.Builder(ClassActivity.this)
                .setTitle("添加课程")
                .setView(view)
                .setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        classInfoDBHelper.addClass(classNameTextView.getText().toString(), classTeacherTextView.getText().toString(), classPlaceTextView.getText().toString(),
                                weekSeqSpinner.getSelectedItemPosition() + 1, classSeqSpinner.getSelectedItemPosition() + 1,
                                classStartSpinner.getSelectedItemPosition() + 1, classEndSpinner.getSelectedItemPosition() + 1,
                                classSemSpinner.getSelectedItemPosition() + 1);
                        classInfoSharedPreferences.addClassNumber(classSemSpinner.getSelectedItemPosition() + 1,
                                classStartSpinner.getSelectedItemPosition() + 1,
                                classEndSpinner.getSelectedItemPosition() + 1,
                                ClassActivity.this);
                        Toast.makeText(ClassActivity.this, "添加" + classNameTextView.getText() + "课程成功", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", null)
                .create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.setting_back_header_blue));
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
    }
}
